<?php

defined('SYSPATH') or die('Access error!');

class Model_Q2a_Post_Tag extends Model
{

    /**
     *  保存对应的问题与标签
     * @param type $question_id
     * @param type $tags_id_array
     */
    public function save_post_tag($question_id, $tags_id_array)
    {
        if (count($tags_id_array) > 0)
        {
            foreach ($tags_id_array as $value)
            {
                $this->insert(array('post_id' => $question_id, 'tag_id' => $value));
            }
        }
    }

    /**
     *  根据问题的id获取关联标签
     * @param type $id
     * @return type
     */
    public function get_question_tag_by_id($id)
    {
        $sql = "SELECT t.*,pt.post_id" .
                " FROM " . $this->table . " AS pt " .
                "LEFT JOIN " . $this->db->get_table('q2a_tag') . " AS t ON pt.tag_id = t.id" .
                " WHERE  pt.post_id = " . $id;
        return (array) $this->db->get_results($sql, ARRAY_A);
    }

    /**
     *  根据标签的id获取关联问题
     * @param type $id
     * @return type
     */
    public function get_question_by_tag_id($id)
    {
        return $this->get_var(array('tag_id'=>$id), 'COUNT(*)');
    }

}